package com.cunyme;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

public class Blackboard_main extends Activity {
	@SuppressWarnings("null")
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
		SQLiteDatabase cuny_meDB = openOrCreateDatabase("cuny_me.db", Context.MODE_PRIVATE, null);
        helper_functions helpers = new helper_functions();
		Map <String, String> blackboard_cookies = helpers.getcookies("blackboard", cuny_meDB);
		cuny_meDB.close();
		
    	ScrollView main_bboard_layout = new ScrollView(this);
    	main_bboard_layout.setBackgroundColor(0xffffffff);
    	LinearLayout child_layout = new LinearLayout(this); 

    	child_layout.setOrientation(LinearLayout.VERTICAL);
    	child_layout.setBackgroundColor(0xffffffff);
    	LinearLayout.LayoutParams bboard_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
    			LinearLayout.LayoutParams.WRAP_CONTENT); 
		main_bboard_layout.addView(child_layout);
		try 
        {  
        	Connection blackboard_menu_conn = Jsoup.connect("https://bbhosted.cuny.edu/webapps/blackboard/content/courseMenu.jsp").data("course_id", getIntent().getExtras().getString("course_id")).cookie("JSESSIONID", blackboard_cookies.get("JSESSIONID")).cookie("session_id", blackboard_cookies.get("session_id")).cookie("ObSSOCookie", blackboard_cookies.get("ObSSOCookie")).cookie("NSC_100211.qspe2_wjq2:443", blackboard_cookies.get("NSC_100211.qspe2_wjq2:443")).cookie("BIGipServerbbhosted_ssl_pl", blackboard_cookies.get("BIGipServerbbhosted_ssl_pl"));
			org.jsoup.nodes.Document blackboard_menu_doc = blackboard_menu_conn.timeout(10*1000).post();
	        Elements blackboard_menu_links = blackboard_menu_doc.select("a[href]");
			final Map <String, String> content_ids = new HashMap <String, String>();
			for (final org.jsoup.nodes.Element link : blackboard_menu_links)
	        {
				if (link.text().equals("Announcements") || link.text().equals("Instructor") || link.text().equals("My Grades"))
				{
	                 final Button menu_button = new Button(this); 
	                 menu_button.setText(link.text());
	                 menu_button.setLayoutParams(bboard_params);
	                 if (link.text().equals("Announcements"))
	                 menu_button.setOnClickListener(new OnClickListener()
	                 {

	             		public void onClick(View v) {
	             			// TODO Auto-generated method stub
	             			  Intent gotoAnnouncements = new Intent(Blackboard_main.this, Blackboard_announcements.class);
	             			  gotoAnnouncements.putExtra("course_id", getIntent().getExtras().getString("course_id"));
	             			  gotoAnnouncements.putExtra("time", "all");
	             			  startActivity(gotoAnnouncements);
	             		}
	                 });
	                 else if (link.text().equals("Instructor"))
	                 {
	                	//https://bbhosted.cuny.edu/bin/common/staff.pl?action=LIST&render_type=DEFAULT&mode=&content_id=_362765_1
	                	 
		        		 String[] content_link_elements = link.attr("abs:href").split("&");
		        		 final String[] content_id_elements = content_link_elements[1].split("=");	
		                 menu_button.setOnClickListener(new OnClickListener()
		                 {

		             		public void onClick(View v) {
		             			// TODO Auto-generated method stub
		          			  Intent gotoInstructor = new Intent(Blackboard_main.this, Blackboard_instructor.class);
		    				  gotoInstructor.putExtra("content_id", content_id_elements[1]);
		        			  startActivity(gotoInstructor);
		             		}
		                 });
	                 }
	                 else if (link.text().equals("My Grades"))
	                 {
		                 menu_button.setOnClickListener(new OnClickListener()
		                 {

		             		public void onClick(View v) {
		             			// TODO Auto-generated method stub
		          			  Intent gotoGrades = new Intent(Blackboard_main.this, Blackboard_my_grades.class);
		        			  gotoGrades.putExtra("course_id", getIntent().getExtras().getString("course_id"));
		        			  startActivity(gotoGrades);
		             		}
		                 });
	                 }
		             child_layout.addView(menu_button);
				}
				
				else if (link.attr("abs:href").indexOf("content_id") >= 0 && !link.text().equals("Getting Started"))
				{
	        		 String[] content_link_elements = link.attr("abs:href").split("&");
	        		 String[] content_id_elements = content_link_elements[1].split("=");	
	        		 content_ids.put(link.text(), content_id_elements[1]);
	        		 
	                 final Button menu_button = new Button(this); 
	                 menu_button.setText(link.text());
	                 menu_button.setLayoutParams(bboard_params);
	                 menu_button.setOnClickListener(new OnClickListener()
	             	{

	     				public void onClick(View v) {
	     					// TODO Auto-generated method stub
	     					
	    					Intent gotoContentPage = new Intent(Blackboard_main.this, Blackboard_content_page.class);
	    					gotoContentPage.putExtra("course_id", getIntent().getExtras().getString("course_id"));
	    					gotoContentPage.putExtra("content_id", content_ids.get(link.text()));
							gotoContentPage.putExtra("is_ann", "no");
	    					startActivity(gotoContentPage);
	     				}
	             	});
	                 child_layout.addView(menu_button);
				}
	        }
	        setContentView(main_bboard_layout);
		}
        catch (IOException e)
        {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(getApplicationContext(), "Unable to Establish Connection.  Please try again.", Toast.LENGTH_SHORT).show();	
			finish();
		}
    }
}
